package com.ctakit.sdk.app.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Target;
import com.squareup.picasso.Transformation;

public class ImgUtil {

    /**
     * 显显毛玻璃效果
     *
     * @param context
     * @param url
     * @param imageView public static void showBlurImg(Context context, String url, ImageView imageView) {
     *                  initImageLoader(context);
     *                  String imgUrl = url;
     *                  if (url != null && !url.startsWith("http")) {
     *                  imgUrl = Config.Server.getImgBaseUrl() + url;
     *                  }
     *                  ImageLoader.getInstance().displayImage(imgUrl, imageView, options_square, blurAnimateFirstDisplayListener);
     *                  }
     **/




    /*public static void loadImg(Context context, String url, ImageView imageView, int placeholderResourceId, int errorResourceId) {
        DrawableTypeRequest<String> imgLoad = Glide.with(context).load(url);
        imgLoad.diskCacheStrategy(DiskCacheStrategy.SOURCE);
        if (placeholderResourceId > 0) {
            imgLoad.placeholder(placeholderResourceId);
        }
        if (errorResourceId > 0) {
            imgLoad.error(errorResourceId);
        }
        imgLoad.into(imageView);
        return;
    }
*/
    public static void loadImg(Context context, String url, ImageView imageView, int placeholderResourceId, int errorResourceId) {
        RequestCreator imgLoad = Picasso.with(context).load(url);
        if (placeholderResourceId > 0) {
            imgLoad.placeholder(placeholderResourceId);
        }
        if (errorResourceId > 0) {
            imgLoad.error(errorResourceId);
        }
        imgLoad.into(imageView);
        return;
    }


    public static void loadImg(Context context, String url, ImageView imageView) {
        // Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
        Picasso.with(context).load(url).into(imageView);
        return;
    }

    public static void loadImg(Context context, int id, ImageView imageView) {
        // Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
        Picasso.with(context).load(id).into(imageView);
        return;
    }

    public static void loadImg(final Context context, String url, final View view) {
        Picasso.with(context).load(url).into(new Target() {

            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                view.setBackground(new BitmapDrawable(context.getResources(), bitmap));
            }

            @Override
            public void onBitmapFailed(final Drawable errorDrawable) {
                Log.d("TAG", "FAILED");
            }

            @Override
            public void onPrepareLoad(final Drawable placeHolderDrawable) {
                Log.d("TAG", "Prepare Load");
            }
        });
        return;
    }


    public static void setNetSelector(final Context context,
                                      final ImageView mImage, final String normalUrl,
                                      final String pressedUrl, final int resId) {
        final StateListDrawable drawable = new StateListDrawable();


        Picasso.with(context).load(pressedUrl).into(new Target() {

            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Drawable draw = new BitmapDrawable(bitmap);
                drawable.addState(
                        new int[]{android.R.attr.state_selected},
                        draw);
                drawable.addState(
                        new int[]{android.R.attr.state_pressed},
                        draw);


                Picasso.with(context).load(normalUrl).into(new Target() {

                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        Drawable draw = new BitmapDrawable(
                                bitmap);
                        drawable.addState(
                                new int[]{android.R.attr.state_enabled},
                                draw);
                        drawable.addState(new int[]{}, draw);

                        mImage.setImageDrawable(drawable);

                    }

                    @Override
                    public void onBitmapFailed(final Drawable errorDrawable) {
                        mImage.setImageResource(resId);
                    }

                    @Override
                    public void onPrepareLoad(final Drawable placeHolderDrawable) {
                        Log.d("TAG", "Prepare Load");
                    }
                });

            }

            @Override
            public void onBitmapFailed(final Drawable errorDrawable) {
                mImage.setImageResource(resId);
            }

            @Override
            public void onPrepareLoad(final Drawable placeHolderDrawable) {
                Log.d("TAG", "Prepare Load");
            }
        });

    }


    /**
     * 对TextView设置不同状态时其文字颜色。
     */
    public static ColorStateList createColorStateList(int normal, int pressed, int focused, int selected) {
        int[] colors = new int[]{pressed, focused, selected, normal};
        int[][] states = new int[4][];
        states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
        states[1] = new int[]{android.R.attr.state_focused, android.R.attr.state_focused};
        states[2] = new int[]{android.R.attr.state_selected};
        states[3] = new int[]{};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

    public static void loadUrlAdaptiveHeight(final Context context, String imageUrl, final ImageView imageView, final int width, int placeholderResourceId, int errorResourceId) {


        Transformation transformation = new Transformation() {

            @Override
            public Bitmap transform(Bitmap source) {
                final int targetWidth = width;

                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                final int targetHeight = (int) (targetWidth * aspectRatio);
                Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                if (result != source) {
                    // Same bitmap is returned if sizes are the same
                    source.recycle();
                }

                imageView.post(new Runnable() {
                    @Override
                    public void run() {
                        final ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                        layoutParams.width = targetWidth;

                        layoutParams.height = targetHeight;
                        imageView.setLayoutParams(layoutParams);
                    }
                });

                return result;
            }

            @Override
            public String key() {
                return "transformation" + " desiredWidth";
            }
        };
        Picasso.with(context)
                .load(imageUrl)
                .placeholder(placeholderResourceId)
                .error(errorResourceId)
                .transform(transformation)
                .into(imageView);

    }

}